import 'package:flutter/services.dart';

class DecimalInputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.text.isEmpty) return newValue;

    var text = newValue.text;
    // 允许数字和最多一个小数点
    if (RegExp(r'^\d*\.?\d*$').hasMatch(text)) {
      return newValue;
    } else {
      return oldValue; // 禁止输入非法字符
    }
  }
}
